package ysy;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.RequiresApi;

import com.example.mytest.R;

import java.util.ArrayList;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public class ImageGridLayout extends GridLayout {

    int maxSize=5;//允许的最大数量
    int lineItemSize=3;//单行展示数量

    ArrayList<ImageView> imageViews=new ArrayList<ImageView>();

    public ImageGridLayout(Context context) {
        super(context);
        addItemView();
    }

    public ImageGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        addItemView();
    }

    public ImageGridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addItemView();
    }

    private void addItemView() {
        for (int i = 0; i < maxSize+1; i++) {
            View view=createView();
            //使用Spec定义子控件的位置和比重
            Log.e("TAG", "addItemView: "+(i/4)+"  "+i%4);
            Spec rowSpec = null;
            Spec columnSpec = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                rowSpec = GridLayout.spec(i/4,1f);
                columnSpec = GridLayout.spec(i%4,1f);
            }

            GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec,columnSpec);
            params.setGravity(Gravity.FILL);
            params.width=WRAP_CONTENT;
            params.height=WRAP_CONTENT;
            params.leftMargin=10;
            params.rightMargin=10;
            params.topMargin=10;
            params.bottomMargin=10;
            addView(view,params);
        }
    }

    private View createView() {
        FrameLayout frameLayout = new FrameLayout(getContext());
        ImageView imageView = createImageView();
        imageViews.add(imageView);
        frameLayout.addView(imageView);
        return frameLayout;
    }

    private ImageView createImageView() {
        ImageView view = new ImageView(getContext());
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
        view.setBackgroundResource(R.color.black);
        view.setImageResource(R.drawable.ic_launcher_background);
        view.setAdjustViewBounds(true);
        view.setScaleType(ImageView.ScaleType.FIT_XY);
        view.setLayoutParams(params);
        return view;
    }


    public void setImageAdaperListener(ImageAdaperListener listener){

    }

    public interface ImageAdaperListener{
        void onImage(ImageView imageView,int pos);
    }

    /**
     * 配合上传图片 处理
     */
    public static class UpImageEntity{
        int state;// 0 未上传  1 已上传  -1 上传失败
    }


}
